package com.example.routelink.gateway.filter;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class EncryptResponseBodyFilterGatewayFilterFactory extends BaseFilter<EncryptResponseBodyFilterGatewayFilterFactory.Config> implements Ordered {
    public EncryptResponseBodyFilterGatewayFilterFactory() {
        super(EncryptResponseBodyFilterGatewayFilterFactory.Config.class);
    }

    public static final String RES_IS_ENCRYPT = "resIsEncrypt";
    public static final String RES_ENCRYPT_KEY = "resEncryptKey";

    @Override
    public GatewayFilter apply(EncryptResponseBodyFilterGatewayFilterFactory.Config config) {
        return (exchange, chain) -> {
            exchange.getAttributes().put(RES_IS_ENCRYPT, true);
            exchange.getAttributes().put(RES_ENCRYPT_KEY, config.secretKey);
            return chain.filter(exchange);
        };
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }


    @Data
    public static class Config {
        private String secretKey;
    }
}
